﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using SWEN3Application.Classes;
using System.IO;

namespace SWEN3Application
{
    public partial class RoomInfo : Form
    {
        private ArrayList roomList;
        private Rooms r;
        internal Action<Rooms> callback;
        String status;

        internal RoomInfo(Action<Rooms> action,String status)
        {
            InitializeComponent();
            FormClosed += CheckRoom_Closed;
            callback = action;
            Label.Visible = true;
            this.status = status;
            if (status == "Vacant")
            {
                Label.Text = "Note: This form is for Check In.\r\rClick on the rows to Check In the room.";
            }
            else if (status == "Occupied")
            {
                Label.Text = "Note: This form is for Check Out.\r\rClick on the rows to Check Out the room.";
            }

        }

        public RoomInfo()
        {
            InitializeComponent();
            RoomDataGridView.CellClick -= RoomDataGridView_CellClick;
            Label.Visible = false;
        }

        private void CheckRoom_Closed(object sender, EventArgs e)
        {
            callback(this.r);
        }

        private void RoomDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            if (i != -1)
            {
                int roomNo = Convert.ToInt32(RoomDataGridView.Rows[i].Cells[0].Value);
                foreach (Rooms r in roomList)
                {
                    if (r.RoomStatus == status && r.RoomNo == roomNo)
                    {
                        this.r = r;
                        this.Close();
                    }
                }
            }
        }

        private void RoomInfo_Load(object sender, EventArgs e)
        {

            RoomDataGridView.ColumnCount = 4;
            RoomDataGridView.Columns[0].Name = "Room Number";
            RoomDataGridView.Columns[1].Name = "Room Rates";
            RoomDataGridView.Columns[2].Name = "Room Type";
            RoomDataGridView.Columns[3].Name = "Room Status";
            RoomDataGridView.Columns[0].Width = 100;
            RoomDataGridView.Columns[1].Width = 100;
            RoomDataGridView.Columns[2].Width = 100;

            roomList = RoomDBManager.GetAllRooms();
            r = null;

            ViewComboBox.SelectedIndex = 0;
        }

        private void ViewComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            setDataGridView(ViewComboBox.SelectedItem.ToString());
        }

        public void setDataGridView(string status)
        {
            Rooms r = new Rooms();
            RoomDataGridView.Rows.Clear();
            
            for (int i = 0; i < roomList.Count; i++)
            {
                r = (Rooms)roomList[i];
                if (r.RoomStatus == status || status == "View All")
                {
                    Object[] row = { r.RoomNo, r.RoomRates, r.RoomType, r.RoomStatus };
                    RoomDataGridView.Rows.Add(row);
                }
            }
            RoomDataGridView.Refresh();

            if (RoomDataGridView.DisplayedRowCount(false) == 0)
            {
                MessageBox.Show("Either all the room is not created or the current staus of the room is not found.");
            }
        }
    }
}
